package com.danbay.framework.protocol.mqtt;

import com.danbay.framework.protocol.command.CommandBuilder;
import com.danbay.framework.protocol.command.ICommandSelector;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author SJG
 *         2017/9/27
 */
@EnableConfigurationProperties(MqttProperties.class)
@Data
public class MqttConfig {

    @Autowired
    private ICommandSelector commandSelector;

    @ConditionalOnMissingBean
    @Bean
    public CommandBuilder getCommandBuilder() {
        return new CommandBuilder();
    }

    @Bean
    public MqttCallback getMqttCallback(ICommandSelector commandSelector, MqttClient mqttClient) {
        MqttCallback callback= new MqttCallback(commandSelector, mqttClient);
        callback.init();
        return callback;
    }

    @Bean
    public MqttClient getMqttClient(MqttProperties mqttProperties) {
        return new MqttClient(mqttProperties);
    }
}
